﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.IO;

namespace VidoopSecure
{
    public class Smsotp
    {
        /// <summary>
        /// SMS One Time Password Identifier.
        /// </summary>
        public string Id
        {
            get { return _id; }
            set { _id = value; }
        }

        /// <summary>
        /// Phone Number to which the SMS One Time Password is sent.
        /// </summary>
        public string Phone
        {
            get { return _phone; }
            set { _phone = value; }
        }

        /// <summary>
        /// Status of sending the SMS One Time Password message.
        /// </summary>
        public string SendStatus 
        {
            get { return _sendStatus; }
            set { _sendStatus = value; }
        }

        /// <summary>
        /// Authentication Status of sending the SMS One Time Password message.
        /// </summary>
        public string AuthenticationStatus 
        {
            get { return _authenticationStatus; }
            set { _authenticationStatus = value; }
        }

        /// <summary>
        /// Generate an <see cref="VidoopSecure.SmsOtp" /> from <paramref name="xml" />.
        /// </summary>
        /// <param name="xml">An XML representation of an 
        /// <see cref="VidoopSecure.SmsOtp" />.</param>
        /// <returns>An <see cref="VidoopSecure.SmsOtp" /> instance.</returns>
        static public Smsotp FromXml(string xml)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            return GenerateSmsotpFromXmlDocument(doc);
        }

        /// <summary>
        /// Generate an <see cref="VidoopSecure.SmsOtp" /> from <paramref name="xml" />.
        /// </summary>
        /// <param name="stream">A <see cref="System.IO.Stream" /> representing the
        /// XML of an <see cref="VidoopSecure.SmsOtp" />.</param>
        /// <returns>An <see cref="VidoopSecure.SmsOtp" /> instance.</returns>
        static public Smsotp FromXml(Stream stream)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(stream);
            return GenerateSmsotpFromXmlDocument(doc);
        }

        static private Smsotp GenerateSmsotpFromXmlDocument(XmlDocument doc)
        {
            Smsotp smsotp = new Smsotp();

            smsotp.Id = doc.GetElementsByTagName("id")[0].InnerText;
            smsotp.Phone = doc.GetElementsByTagName("phone")[0].InnerText;
            smsotp.SendStatus = doc.GetElementsByTagName(
                "send_status")[0].InnerText;
            smsotp.AuthenticationStatus = doc.GetElementsByTagName(
                "authentication_status")[0].InnerText;

            return smsotp;
        }

        private string _id;
        private string _phone;
        private string _sendStatus;
        private string _authenticationStatus;
    }
}
